package org.linlinjava.litemall.wx.web;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallTender;
import org.linlinjava.litemall.db.domain.LitemallTenderMsg;
import org.linlinjava.litemall.db.service.LitemallCustomerService;
import org.linlinjava.litemall.db.service.LitemallTenderQuoteService;
import org.linlinjava.litemall.db.service.LitemallTenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 投标招标书
 * @program: mall
 * @author: youvy
 * @create: 2019-12-26 14:02
 **/
@RestController
@RequestMapping("/tender")
public class WxTenderController {
	@Autowired
	LitemallTenderService tenderService;
	@Autowired
	LitemallCustomerService customerService;
	@Autowired
	private LitemallTenderQuoteService tenderQuoteService;

	/**
	 * 查:所有的投标书(符合条件的)
	 * @param json   page pageSize status
	 * @param request 登陆状态
	 * @return
	 */
	@PostMapping("wx/wxList")
	public Object wxList(@RequestBody String json, HttpServletRequest request) {
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		Integer userid = Integer.parseInt(user.getUserid());
		Integer page = JacksonUtil.parseInteger(json, "page");
		Integer pageSize = JacksonUtil.parseInteger(json, "pageSize");
		String statusStr = JacksonUtil.parseString(json, "status");
		Integer status = null;
		if (!StringUtils.isEmpty(statusStr)){
			status=Integer.parseInt(statusStr);
		}

		List<LitemallTender> list = tenderService.list(page, pageSize, userid, null,status);
		return ResponseUtil.okList(list);

	}

	/**
	 *  商户 List
	 * @param json  page  pageSize   belong是brandId商户ID
	 * @param request  请求状态
	 * @return
	 */
	@PostMapping("brandList")
	public Object brandList(@RequestBody String json,HttpServletRequest request) {
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		String belong = user.getPlugininfo().get("belong");
		Integer page = JacksonUtil.parseInteger(json, "page");
		Integer pageSize = JacksonUtil.parseInteger(json, "pageSize");
		List<LitemallTenderMsg> list = tenderService.brandList(page, pageSize,Integer.parseInt(belong));
		Integer count = tenderService.brandListCount(Integer.parseInt(belong));
		return ResponseUtil.countList(list,count);
	}

	/**
	 *  增: 投标书
	 * @param tender  投标书
	 * @param request 请求状态
	 * @return
	 */
	@PostMapping("wx/insert")
	public Object insert(@RequestBody LitemallTender tender, HttpServletRequest request) {
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		Integer userid = Integer.parseInt(user.getUserid());
		tender.setUserId(userid);
		tenderService.insert(tender);
		return ResponseUtil.ok();
	}

	/**
	 * 更新:投标书
	 * @param tender 投标书
	 * @return
	 */
	@PostMapping("wx/update")
	public Object update(@RequestBody LitemallTender tender) {
		tenderService.update(tender);
		return ResponseUtil.ok();
	}

	/**
	 * 删除:投标书
	 * @param json  投标书 id
	 * @return
	 */
	@PostMapping("wx/delete")
	public Object delete(@RequestBody String json) {
		Integer id = JacksonUtil.parseInteger(json, "id");
		tenderService.delete(id);
		return ResponseUtil.ok();
	}
}
